import { NTabs, type TabsProps, type TabsInst } from "naive-ui";
import type { HTMLAttributes } from "vue";
import { getSlotsDom } from "../utils";
import type { BaseComponentType } from "../types";

export type TabsFactoryConstructorType = {
  attrs?: HTMLAttributes;
  props?: TabsProps;
  defaultSlot?: BaseComponentType;
  prefixSlot?: BaseComponentType;
  suffixSlot?: BaseComponentType;
};

export class TabsFactory {
  private attrs: HTMLAttributes = {};
  private props: TabsProps = {};
  private defaultSlot: BaseComponentType = null;
  private prefixSlot: BaseComponentType = null;
  private suffixSlot: BaseComponentType = null;
  private domRef: TabsInst | null = null;

  constructor(data?: TabsFactoryConstructorType) {
    if (data?.attrs) this.setAttrs(data.attrs);
    if (data?.props) this.setProps(data.props);
    if (data?.defaultSlot) this.setDefault(data.defaultSlot);
    if (data?.prefixSlot) this.setPrefix(data.prefixSlot);
    if (data?.suffixSlot) this.setSuffix(data.suffixSlot);
  }

  setAttrs(attrs: HTMLAttributes) {
    this.attrs = attrs;
    return this;
  }

  setProps(props: TabsProps) {
    this.props = props;
    return this;
  }

  setDefault(component: BaseComponentType) {
    this.defaultSlot = component;
    return this;
  }

  setPrefix(component: BaseComponentType) {
    this.prefixSlot = component;
    return this;
  }

  setSuffix(component: BaseComponentType) {
    this.suffixSlot = component;
    return this;
  }

  syncBarPosition() {
    this.domRef?.syncBarPosition();
  }

  create() {
    return (
      <NTabs
        ref={(el: HTMLElement) => {
          this.domRef = el as unknown as TabsInst;
        }}
        {...this.attrs}
        {...this.props}
      >
        {{
          default:
            this.defaultSlot != null
              ? () => getSlotsDom(this.defaultSlot)
              : null,
          prefix:
            this.prefixSlot != null ? () => getSlotsDom(this.prefixSlot) : null,
          suffix:
            this.suffixSlot != null ? () => getSlotsDom(this.suffixSlot) : null,
        }}
      </NTabs>
    );
  }
}
